/** @brief File containing the implementation of the basic square grid.
 *
 * This file contains the implementation of the simplest grid visualisation:
 * the cells are squares.
 *
 * @file SquareRepresentation.cpp
 * @author Johan "Solo" Luisier
 * @date 2013/03/26
 */
#include "SquareRepresentation.hpp"

#include "Core/GridBase.hpp"

#include <QtCore/QDebug>
#include <QtCore/qmath.h>
#include <QtCore/QPoint>
#include <QtCore/QPointF>

#include <QtSvg/QGraphicsSvgItem>
#include <QtSvg/QSvgRenderer>

const qreal SquareRepresentation::TileDimension( 10. );

SquareRepresentation::SquareRepresentation( GridBase *grid, QObject *parent )
    : RepresentationBase( grid, parent )
    , TopLeftPosition( 0., 0. )
    , BottomRightPosition( TileDimension * grid -> width(), TileDimension * grid -> height() )
{
    setupTiles();
}

SquareRepresentation::~SquareRepresentation()
{}

void SquareRepresentation::retrieveCell( const QPointF& pt )
{
    int i( qFloor( pt.x() / tileWidth() ) );
    int j( qFloor( pt.y() / tileHeight() ) );

    qDebug() << "Touched cell" << i << j;

    emit( touchedCell( QPoint( i, j ) ) );
}

void SquareRepresentation::setupTiles()
{
    quint32 i, j;

    QSvgRenderer* renderer = new QSvgRenderer( QString( ":/tiles" ) );

    for ( j = 0u; j < GridModel -> height(); j++ )
    {
        for ( i = 0u; i < GridModel -> width(); i++ )
        {
            QGraphicsSvgItem* tile = new QGraphicsSvgItem();
            tile -> setSharedRenderer( renderer );
            tile -> setElementId( QString( "BlueSquare" ) );
            tile -> setPos( i * TileDimension, j * TileDimension );
            tile -> hide();
            addItem( tile );
            CellTiles.append( tile );
        }
    }
}
